package com.asion.vmovie.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.adapters.NewsViewAdapter;
import com.asion.vmovie.adapters.PingDaoDetailAdapter;
import com.asion.vmovie.beans.DetialsBean;
import com.asion.vmovie.ui.MVPContract.PingDaoDetialContract;
import com.asion.vmovie.ui.MVPModel.PingDaoDetialModel;
import com.asion.vmovie.ui.MVPPresenter.PingDaoDetialPresenter;
import com.asion.vmovie.utils.ToastUtil;

import butterknife.BindView;

/**
 * Created by asion on 16-10-19.
 */

public class PingDaoDetialActivity extends BaseActivity<PingDaoDetialPresenter,PingDaoDetialModel> implements PingDaoDetialContract.View, NewsViewAdapter.OnItemClickListener {


    @BindView(R.id.home_side)
    ImageView homeSide;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recycler_title)
    LinearLayout recyclerTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout refreshlayout;
    private PingDaoDetailAdapter pingDaoDetailAdapter;

    public static void showAction(Activity activity, Bundle bundle) {
        Intent intent = new Intent(activity, PingDaoDetialActivity.class);
        intent.putExtra("bundle", bundle);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                R.anim.fade_out);
    }


    @Override
    public int getLayoutId() {
        return R.layout.recycler_fragment;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
    }

    @Override
    public void initView() {
        startProgressDialog();
        recyclerTitle.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        String string = bundle.getString("title");
        String id = bundle.getString("cateid");
        this.title.setText(string);

        pingDaoDetailAdapter = new PingDaoDetailAdapter(this);
        recycler.setItemAnimator(new DefaultItemAnimator());
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(pingDaoDetailAdapter);

        refreshlayout.setOnRefreshListener(()->{
            page=0;
            pingDaoDetailAdapter.removeAll();
            new Handler().postDelayed(()->refreshlayout.setRefreshing(false),2000);
        });

        homeSide.setOnClickListener(v->finish());
        pingDaoDetailAdapter.setOnItemClickListener(this);

        mPresenter.getPingDaoDetialData(1,10,id);

        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int itemCount = layoutManager.getItemCount();
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (!loading&&itemCount<(lastVisibleItemPosition+2)) {
                    mPresenter.getPingDaoDetialData(++page,10,id);
                    loading=true;
                }
            }
        });
    }

    private boolean loading = false;
    private int page = 1;

    @Override
    public void showDataSuccess(DetialsBean detialsBean) {
        pingDaoDetailAdapter.addAll(detialsBean.getData());
        stopProgressDialog();
        loading = false;
    }

    @Override
    public void showDataError(Throwable throwable) {
        ToastUtil.showShort("网络请求失败");
        loading=false;
    }

    @Override
    public void onItemClick(DetialsBean.DataBean dataBean) {
        Bundle bundle = new Bundle();
        bundle.putString("postid",dataBean.getPostid());
        VideoActivity.showAction(this,bundle);
    }
}
